 package com.taobao.diamond.utils;

 import java.io.File;
 import java.util.Enumeration;
 import java.util.Properties;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.log4j.Appender;
 import org.apache.log4j.FileAppender;
 import org.apache.log4j.Logger;
 import org.apache.log4j.PropertyConfigurator;

 public class LoggerInit
 {
   public static final String LOG_NAME_CONFIG_DATA = "DiamondConfigDataLog";
   public static final Log log = LogFactory.getLog(LoggerInit.class);

   private static volatile boolean initOK = false;

   private static Properties defaultProperties = new Properties();

   public static void initLogFromBizLog()
   {
     if (initOK) {
       return;
     }

     ClassLoader cl = Thread.currentThread().getContextClassLoader();
     Thread.currentThread().setContextClassLoader(LoggerInit.class.getClassLoader());
     try
     {
       PropertyConfigurator.configure(defaultProperties);

       FileAppender bizFileAppender = getFileAppender(Logger.getRootLogger());
       if (null == bizFileAppender) {
         log.warn("上层业务层没有在ROOT LOGGER上设置FileAppender!!!");
         bizFileAppender = new FileAppender();
         bizFileAppender.setFile(System.getProperty("user.home") + "/diamond/logs/diamond_config_data.log");
       }

       setFileAppender(bizFileAppender, "DiamondConfigDataLog");

       initOK = true;
     }
     finally {
       Thread.currentThread().setContextClassLoader(cl);
     }
   }

   private static void setFileAppender(FileAppender bizFileAppender, String logName)
   {
     FileAppender fileAppender = getFileAppender(Logger.getLogger(logName));
     String bizLogDir = new File(bizFileAppender.getFile()).getParent();

     File newLogFile = new File(bizLogDir, fileAppender.getFile());

     fileAppender.setFile(newLogFile.getAbsolutePath());
     fileAppender.activateOptions();
     log.warn("成功为" + logName + "添加Appender. 输出路径:" + newLogFile.getAbsolutePath());
   }

   private static FileAppender getFileAppender(Logger logger)
   {
     FileAppender fileAppender = null;
     for (Enumeration appenders = logger.getAllAppenders(); (null == fileAppender) && (appenders.hasMoreElements()); ) {
       Appender appender = (Appender)appenders.nextElement();
       if (FileAppender.class.isInstance(appender)) {
         fileAppender = (FileAppender)appender;
       }
     }
     return fileAppender;
   }

   static
   {
     defaultProperties.put("log4j.logger.DiamondConfigDataLog", "info, DiamondConfigDataLogFile");
     defaultProperties.put("log4j.additivity.DiamondConfigDataLog", "false");
     defaultProperties.put("log4j.appender.DiamondConfigDataLogFile", "org.apache.log4j.DailyRollingFileAppender");
     defaultProperties.put("log4j.appender.DiamondConfigDataLogFile.DatePattern", "'.'yyyy-MM-dd");
     defaultProperties.put("log4j.appender.DiamondConfigDataLogFile.File", "diamond_config_data.log");
     defaultProperties.put("log4j.appender.DiamondConfigDataLogFile.layout", "org.apache.log4j.PatternLayout");
     defaultProperties.put("log4j.appender.DiamondConfigDataLogFile.layout.ConversionPattern", "%d{MM-dd HH:mm:ss} - %m%n");

     defaultProperties.put("log4j.appender.DiamondConfigDataLogFile.Append", "true");
   }
 }
